home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume6 / att_which next >
Encoding:
Internet Message Format  |  1986-11-30  |  4.3 KB

  1. Subject:  v06i105:  A "which" for non-BSD systems (att_which)
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.UUCP
  4.  
  5. Submitted by: Paul Vixie <pyrnj!pyramid!hplabs!hpsemc!vix>
  6. Mod.sources: Volume 6, Issue 105
  7. Archive-name: att_which
  8.  
  9. [  I threw together a manpage.  This program doesn't handle aliases,
  10.    but if you had aliases you wouldn't need this program, would you?
  11.    --r$  ]
  12.  
  13.  
  14. #!/bin/sh
  15. # This is a shell archive.  Remove anything before this line,
  16. # then unpack it by saving it in a file and typing "sh file".
  17. # Contents:  README which.1 Makefile which.c
  18.  
  19. echo x - README
  20. if test -f README ; then
  21.     echo README exists, putting output in $$README
  22.     OUT=$$README
  23. else
  24.     OUT=README
  25. fi
  26. sed 's/^XX//' > $OUT <<'@//E*O*F README//'
  27. XX26-July-1985
  28. XXVersion 1.0
  29.  
  30. XXThis is another C implementation of the 'which' command, written because
  31. XX(a) is was easy, (b) I didn't know how to find the original, and (c) HP-UX
  32. XXon the 9000 series 300 and 500 doesn't have it.
  33.  
  34. XXTo install, unshar into an empty directory, edit the Makefile to change the
  35. XXDESTDIR (probably to /usr/local, /usr/local/bin or /usr/ucb), then su and
  36. XXtype 'make install'.  If all goes well type 'make clean' or even 'rm *' if
  37. XXyou saved the distribution kit.
  38.  
  39. XXQuestions, problems, flames, to:
  40.  
  41. XX    Paul Vixie
  42. XX    ucbvax!dual!qantel!vixie!paul
  43. @//E*O*F README//
  44. chmod u=rw,g=rw,o= README
  45.  
  46. echo x - which.1
  47. if test -f which.1 ; then
  48.     echo which.1 exists, putting output in $$which.1
  49.     OUT=$$which.1
  50. else
  51.     OUT=which.1
  52. fi
  53. sed 's/^XX//' > $OUT <<'@//E*O*F which.1//'
  54. XX.TH WHICH 1 LOCAL
  55. XX.SH NAME
  56. XXwhich \- show full path of commands
  57. XX.SH SYNOPSIS
  58. XX.B which
  59. XXprogname ...
  60. XX.SH DESCRIPTION
  61. XX.I Which
  62. XXtakes a series of program names, and prints
  63. XXout the full pathname of the program that the shell would call to
  64. XXexecute it.
  65. XXIt does this by simulating the shells searching of the
  66. XX.B $PATH
  67. XXenvironment variable.
  68. XX.SH "SEE ALSO"
  69. XXThe exec(2,3) family.
  70. @//E*O*F which.1//
  71. chmod u=rw,g=rw,o=rw which.1
  72.  
  73. echo x - Makefile
  74. if test -f Makefile ; then
  75.     echo Makefile exists, putting output in $$Makefile
  76.     OUT=$$Makefile
  77. else
  78.     OUT=Makefile
  79. fi
  80. sed 's/^XX//' > $OUT <<'@//E*O*F Makefile//'
  81. XX# makefile for 'which'
  82. XX# vix 25jul86 [written]
  83.  
  84. XXCFLAGS        =    -O
  85. XXDESTDIR        =    /mnt/local/bin
  86.  
  87. XX.c.o        :
  88. XX            cc $(CFLAGS) -c $<
  89.  
  90. XXall        :    which
  91.  
  92. XXwhich        :    which.o
  93. XX            cc -o which which.o
  94.  
  95. XXwhich.o        :    which.c
  96.  
  97. XXinstall        :    all
  98. XX            mv which $(DESTDIR)
  99. XX            chmod 775 $(DESTDIR)/which
  100.  
  101. XXclean        :
  102. XX            rm -f which.o
  103.  
  104. XXkit        :    clean
  105. XX            shar -bcsv README Makefile which.c > which.shar
  106. @//E*O*F Makefile//
  107. chmod u=rw,g=rw,o= Makefile
  108.  
  109. echo x - which.c
  110. if test -f which.c ; then
  111.     echo which.c exists, putting output in $$which.c
  112.     OUT=$$which.c
  113. else
  114.     OUT=which.c
  115. fi
  116. sed 's/^XX//' > $OUT <<'@//E*O*F which.c//'
  117. XX/* which - C version of the unix/csh 'which' command
  118. XX * vix 23jul86 [written]
  119. XX * vix 24jul86 [don't use dynamic memory]
  120. XX */
  121.  
  122. XX#include <stdio.h>
  123.  
  124. XXstatic    char    *myname;
  125.  
  126. XXmain(argc, argv)
  127. XXint    argc;
  128. XXchar    *argv[];
  129. XX{
  130. XX    char    *getenv(), *path = getenv("PATH");
  131.  
  132. XX    myname = argv[0];
  133. XX    for (argc--, argv++;  argc;  argc--, argv++)
  134. XX        if (0 != which(*argv, path))
  135. XX            exit(1);
  136. XX    exit(0);
  137. XX}
  138.  
  139. XXstatic which(name, path)
  140. XXchar    *name, *path;
  141. XX{
  142. XX    char    test[1000], *pc, *malloc(), save;
  143. XX    int    len, namelen = strlen(name), found;
  144.  
  145. XX    pc = path;
  146. XX    found = 0;
  147. XX    while (*pc != '\0' && found == 0)
  148. XX    {
  149. XX        len = 0;
  150. XX        while (*pc != ':' && *pc != '\0')
  151. XX        {
  152. XX            len++;
  153. XX            pc++;
  154. XX        }
  155.  
  156. XX        save = *pc;
  157. XX        *pc = '\0';
  158. XX        sprintf(test, "%s/%s", pc-len, name);
  159. XX        *pc = save;
  160. XX        if (*pc)
  161. XX            pc++;
  162.  
  163. XX        found = (0 == access(test, 01));    /* executable */
  164. XX        if (found)
  165. XX            puts(test);
  166. XX    }
  167. XX    if (found == 0)
  168. XX    {
  169. XX        printf("%s: no %s in (%s)\n", myname, name, path);
  170. XX        return 1;
  171. XX    }
  172. XX    return 0;
  173. XX}
  174. @//E*O*F which.c//
  175. chmod u=rw,g=rw,o=rw which.c
  176.  
  177. echo Inspecting for damage in transit...
  178. temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
  179. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  180. cat > $temp <<\!!!
  181.       16      90     558 README
  182.       16      66     368 which.1
  183.       25      57     354 Makefile
  184.       57     152     925 which.c
  185.      114     365    2205 total
  186. !!!
  187. wc  README which.1 Makefile which.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  188. if test -s $dtemp
  189. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  190. else echo "No problems found."
  191. fi
  192. exit 0
  193.